unit StopWatch;

interface

uses Windows, SysUtils;

type
  TStopWatch = class
  private
    FStartCount: Int64;
    FFrequency: Int64;
    function GetTime: Double;
  public
    constructor Create;
    procedure Reset;
    property Time: Double read GetTime;
    { Returns the time as string }
    function ToString: string;
      {$IF CompilerVersion < 20} virtual; {$ELSE} override; {$IFEND}
  end;

var
  Watch: TStopWatch;

procedure WatchInterval(Text: string = '');

implementation


procedure WatchInterval(Text: string = '');
begin
  if Text <> '' then
    Writeln(Text + ': ' + Watch.ToString);
  Watch.Reset;
end;

{ TStopWatch }

constructor TStopWatch.Create;
begin
  QueryPerformanceFrequency(FFrequency);
  Reset;
end;

function TStopWatch.GetTime: Double;
var
  EndCount: Int64;
begin
  QueryPerformanceCounter(EndCount);
  Result := (EndCount - FStartCount) / FFrequency;
end;

procedure TStopWatch.Reset;
begin
  QueryPerformanceCounter(FStartCount);
end;

function TStopWatch.ToString: string;
begin
  Result := Format('%.3f msec', [Time * 1000]);
end;

initialization
  Watch := TStopWatch.Create;
finalization
  Watch.Free;
end.
